package com.app.controller;

import com.app.common.SystemConstant;
import com.app.enums.StatusCodeEnum;
import com.app.model.UserModel;
import com.app.service.UserService;
import com.app.util.*;
import com.app.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;


@RestController
@Controller
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;

    //获取openid
    @GetMapping("/getOpenid")
    public Result<UserLoginVo> getOpenid(String code, HttpServletRequest request) throws LoginException {
        //微信登录
        UserModel user = userService.getOpenid(code, request);
        //生成token
        String token = TokenUtils.sign(user.getOpenId());
        UserLoginVo userLoginVO = UserLoginVo.builder()
                .user(user)
                .token(token)
                .build();
        System.out.println(token);
        return Result.success(userLoginVO);
    }


    //获取手机号
    @GetMapping("/getPhone")
    public Result<Object> getPhone(String code, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS401.getCode(), StatusCodeEnum.STATUS401.getMsg());
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
        String phone = userService.getPhone(code, request);
        return Result.success(phone);
    }


    //更新用户
    @PostMapping("/insertUser")
    public Result<Object> insertUser(@RequestBody UserModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS401.getCode(), StatusCodeEnum.STATUS401.getMsg());
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
        model.setId(user.getId());
        model.setUpdateTime(CommonHelp.getNowTimestamp());
        userService.update(model);
        return Result.success("操作成功");
    }

    //我的资料
    @GetMapping("/myProfile")
    public Result<Object> myProfile(Integer id) {
        return Result.success(userService.selectById(id));
    }

    //我的发布
    @GetMapping("/myPublishing")
    public Result<Object> myPublishing(HttpServletRequest request, Integer type, Integer status) {//1创业蓝海2乡村集市
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS401.getCode(), StatusCodeEnum.STATUS401.getMsg());
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
        return Result.success(userService.getPublishById(user.getId(), type, status));
    }


    //查询个人信息
    @GetMapping("/selectUserById")
    public Result<Object> selectUserById(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS401.getCode(), StatusCodeEnum.STATUS401.getMsg());
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
        return Result.success(userService.selectUserById(user.getId()));
    }
    //获取小程序二维码
    @PostMapping("/getCode")
    public Result<Object> getCode(HttpServletRequest request) throws Exception {
            return  Result.success(WxQrCode.generateQrCode( "C:/upload/"+ "小程序码"+".png","pages/index/index",WxQrCode.postToken(WXConstant.APPID,WXConstant.SECRET)));
    }

}
